package com.fengzai.demo.config;

import com.fengzai.netty.common.callback.HandlerProcessor;
import com.fengzai.netty.common.domain.HttpRequestData;
import com.fengzai.netty.common.http.HttpRequestParamConvert;
import com.fengzai.netty.common.http.HttpResponseBuilder;
import com.fengzai.netty.http.DefaultHttpServer;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @PACKAGE_NAME: com.fengzai.demo.config
 * @author: rhf
 * @ProjectName: netty
 * @DATE: 2021/10/18
 **/
@Configuration
public class HttpConfiguration {
    /**
     * 服务端ip
     */
    private Integer servicePort = 10004;

    @Bean(destroyMethod = "stop")
    public DefaultHttpServer httpServer() {
        HandlerProcessor<FullHttpRequest, FullHttpResponse> handlerProcessor = fullHttpRequest -> {
            try {
                HttpRequestData httpRequestDataBean = HttpRequestParamConvert.convertData(fullHttpRequest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            FullHttpResponse response = HttpResponseBuilder.build("ok");
            return response;
        };
        DefaultHttpServer httpServer = new DefaultHttpServer(handlerProcessor);
        new Thread() {
            @Override
            public void run() {
                httpServer.run(servicePort);
            }
        }.start();
        return httpServer;
    }
}
